package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Process;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.SysDictData;
import com.qidianit.mes.pojo.WorkflowProcess;
import com.qidianit.mes.pro.mapper.DictDataMapper;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.WorkflowProcessMapper;
import com.qidianit.mes.pro.service.WorkflowProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/18 -18:50
 */
@Service
public class WorkflowProcessServiceImpl implements WorkflowProcessService {
    @Autowired
    private WorkflowProcessMapper workflowProcessMapper;

    @Autowired
    private ProcessMapper processMapper;

    @Autowired
    private DictDataMapper dictDataMapper;
    @Override
    public Result<PageInfo<WorkflowProcess>> queryList(Long workflowId, int page, int size) {
        //使用分页插件
        PageHelper.startPage(page,size);
        //查询数据
        List<WorkflowProcess> workflowProcesses = workflowProcessMapper.queryWorkflowProcessList(workflowId);

        for (int i = 0; i < workflowProcesses.size(); i++) {
            //当前的工序
            Process process = processMapper.queryById(workflowProcesses.get(i).getProcessId());
            workflowProcesses.get(i).setProcess(process);
            //查询下一道工序
            Process nextProcess = getNextProcess(workflowProcesses, workflowProcesses.get(i));
            workflowProcesses.get(i).setNextProcess(nextProcess);
//           if(i+1< workflowProcesses.size()){
//               Process process1 = processMapper.queryById(workflowProcesses.get(i + 1).getProcessId());
//               workflowProcesses.get(i).setNextProcess(process1);
//           }

            //工序之间是否存在关系
            if (workflowProcesses.get(i).getRelationNext()!=null){
                SysDictData sysDictData = dictDataMapper.queryByDictDataId(workflowProcesses.get(i).getRelationNext());
                workflowProcesses.get(i).setRelationNextDict(sysDictData);
            }

        }
        return Result.success(new PageInfo<>(workflowProcesses));
    }

    @Override
    public Result addWorkflowProcess(WorkflowProcess process) {
        //序号是否已存在
        //工序是否已存在
        List<WorkflowProcess> workflowProcesses =
                workflowProcessMapper.queryWorkflowProcessList(process.getWorkflowId());
        for (WorkflowProcess workflowProcess : workflowProcesses) {
            //检查序号是否存在
            if(Objects.equals(workflowProcess.getStepOrder(), process.getStepOrder())){
                String msg ="工序序号["+process.getStepOrder()+"]已经存在";
                return Result.error(msg);
            }
            //检查工序是否已经存在
            if (Objects.equals(workflowProcess.getProcessId(),process.getProcessId())){
                Process process1 = processMapper.queryById(process.getProcessId());
                String msg =  "工序"+process1.getProcessName()+"已经存在";
                return Result.error(msg);
            }
        }
        return workflowProcessMapper.saveWorkflowProcess(process)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result<WorkflowProcess> getById(Long id) {
        WorkflowProcess workflowProcess = workflowProcessMapper.queryWPId(id);
        workflowProcess.setProcess(processMapper.queryById(workflowProcess.getProcessId()));
        if (workflowProcess.getRelationNext()!=null){
            SysDictData sysDictData = dictDataMapper.queryByDictDataId(workflowProcess.getRelationNext());
            workflowProcess.setRelationNextDict(sysDictData);
        }
        return Result.success(workflowProcess);
    }

    @Override
    public Result updateWorkflowProcess(WorkflowProcess workflowProcess) {
        //校验序号是否存在
        //查询工艺的所有工序
        List<WorkflowProcess> workflowProcesses =
                workflowProcessMapper.queryWorkflowProcessList(workflowProcess.getWorkflowId());
        //遍历查询
        for (WorkflowProcess process : workflowProcesses) {
            //已经存在序号，并且并不是同一个工序组成
            if(Objects.equals(workflowProcess.getStepOrder(), process.getStepOrder())&&
                    !workflowProcess.getId().equals(process.getId())){
                String msg ="工序序号["+workflowProcess.getStepOrder()+"]已经存在";
                return Result.error(msg);
            }
            //检查工序是否已经存在,并且并不是同一个工序组成
            if (Objects.equals(workflowProcess.getProcessId(),process.getProcessId())&&
                    !workflowProcess.getId().equals(process.getId())){
                Process process1 = processMapper.queryById(process.getProcessId());
                String msg =  "工序"+process1.getProcessName()+"已经存在";
                return Result.error(msg);
            }
        }
        return workflowProcessMapper.updateWorkflowProcess(workflowProcess)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result deleteWorkflowProcessById(Long id) {

        return workflowProcessMapper.deleteWorkflowProcessById(id)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDeleteWorkflowProcess(Long[] ids) {
        return workflowProcessMapper.batchDeleteWorkflowProcess(ids)>0?Result.success():Result.error("操作失败");
    }


    //找到下一道工序的方法
    private Process getNextProcess(List<WorkflowProcess> list,WorkflowProcess workflowProcess){
        //返回的Process数据
        Process process = null;
        //当前工序的序号
        Integer order = workflowProcess.getStepOrder();
        //寻找下一个序号
        for (WorkflowProcess workflowProcess1 : list) {
            System.out.println(workflowProcess1.getStepOrder());
            if (workflowProcess1.getStepOrder().equals(order+1)){
                process=processMapper.queryById(workflowProcess1.getProcessId());
                break;
            }
        }
        System.out.println("下一道工序："+process);
        return process;
    }
}
